var vue;
var vue2;
var vue3;
function vue_init() {
    vue = new Vue({
        delimiters: ['{!', '!}'],
        el: "#market-app",
        data: {
			assets:1000,//资产
			assetsTyple:'BNB',//资产类型
			inputNum:'',
			wrongtext:'',
			newAddress:true,
            inputAddress:'',
            CoinDetail: {},     /* 详细数据 */
            coin_type: "",  /* 类型 */
            img_url: "",    /* 图标 */
            wid_detail: {},     /* 提现页面内容 */
            withdraw_address: [],   /* 请求回来的地址 */
            add_address: {},    /* 新增的地址 */
        },
        methods:{
            init: function () {
                var params = get_url_params()
                // console.log(" get coin_type " + params['coin_type'])
                // console.log(params.coin_type);
                this.get_coin_type(params.coin_type);
                this.coin_type = params.coin_type;
                this.get_withdrawal(this.coin_type);
                /* 调用函数，获取地址列表 */
                this.get_withdraw_address(this.coin_type);
                
            },
			openview:function(){
				layer.open({
					className:'depositlist',
					type: 1,
					content: $("#testbox").html(),
					anim: 'up',
					style: 'background: #12161C;color: #f3f3f3;position:fixed; left:0; top:0; width:100%; height:100%; border: none; -webkit-animation-duration: .5s; animation-duration: .5s;'
				});
				vue3_init();
			},
			open_Prompt:function(){
				layer.open({
					className:'PromptBox',
					type: 1,
					content: $("#Prompt").html(),
					anim: 'up',
					style: 'background:none;color: #f3f3f3;position:fixed; left:0; top:0; width:100%; height:100%; border: none; -webkit-animation-duration: .5s; animation-duration: .5s;'
				});
                //this.drawQRcode();
                /* 提示最小提现 */
                $("#wid_min").html("• 最小提现数量: &nbsp"+ this.wid_detail.min + this.CoinDetail.name_upper)
			},
			check_input:function(e){
				
                var input_val=e.target.value;
                // console.log(123)
                // console.log(this.inputNum)
				if(input_val == '' || input_val == null){
					this.wrongtext= '请输入金额';
					this.inputNum= '';
				}else{
					this.wrongtext= '';
					if(input_val < 0 ){
						this.wrongtext= '格式不正确';
						this.inputNum= '';
					}
					if(input_val > this.wid_detail.amount){
						this.wrongtext= '余额不足';
						this.inputNum= '';
					}
					if(input_val < this.wid_detail.min){
						this.wrongtext= '最少提现为' + this.wid_detail.min + this.CoinDetail.name_upper;
						this.inputNum= '';
					}
					//this.inputNum=input_val;
					//console.log(this.inputNum);
				}
				
			},
			change_address:function(){
				this.newAddress=!this.newAddress;
			},
			check_address:function(e){
				var input_val=e.target.value;
				if(input_val=='' || input_val==null){
					this.inputAddress='';
				}else{
					//this.inputAddress=input_val;
					//console.log(this.inputNum+this.inputAddress);
				}	
			},
			all_to_cash:function(){
				this.inputNum = this.wid_detail.amount;
			},
			openAddressForm:function(){
                var coin_type = this.coin_type
				layer.open({
					className:'addressForm',
					type: 1,
					content: $("#addressForm").html(),
					anim: 'up',
					style: 'padding-top: 2.8rem;background: rgba(18, 22, 28, 1);box-sizing: border-box;color: #f3f3f3;position:fixed; left:0; top:0; width:100%; height:100%; border: none; -webkit-animation-duration: .5s; animation-duration: .5s;',
					success: function(elem){
					//   console.log(elem);
					  vue2_init(coin_type);
					  vue2.$data.assetsTyple=this.assetsTyple;//VUE间传值
					  vue.change_address();
					}    
				});
				//var appendHtml=$("#addressForm").html("<h3>{{assets}}</h3>");
                //this.$compile(appendHtml.get(0));
                /* 把类型填到新增地址页 */
                /* 修改名字 */
                $("#add_name_upper").html(this.CoinDetail.name_upper);
                /* 修改全称 */
                $("#add_coin_type").html(this.CoinDetail.full_name);
                /* 修改图标 */
                $("#add_coin_img").attr("src", this.CoinDetail.img);
				
            },
            /* 根据类型获取图标和名称 */
            get_coin_type: function (coin_type) { 
                /* 发送请求 */
                $.get("/m/get_balance", {
                    coin_type: coin_type
                }, (data) => {
                    this.CoinDetail = data;
                    // console.log(this.CoinDetail);
                    /* 图标类型 */
                    var coin_info = get_coin_info(data.coin_type);
                    /* 图标样式 */
                    this.CoinDetail.img = get_coin_icon_url(data.coin_type);
                    this.CoinDetail.name_upper = coin_info.name_upper;
                    this.CoinDetail.full_name = coin_info.cname;
                });
            },

            /* 根据类型获取提现数据 */
            get_withdrawal: function (coin_type) {
                /* 发送请求 */
                $.post("/btc/withdraw", {
                    coin_type: coin_type
                }, (data) => {
                    // console.log(data);
                    this.wid_detail = data;
                })
            },

            /* 获取地址 */
            get_withdraw_address: function (coin_type) {
                $.post("/btc/get_withdraw_address", {
                    coin_type: coin_type,
                }, (res) => {
                    // console.log(res);
                    this.withdraw_address = res;
                    this.push_address(res.length - 1);
                });
            },

            /* 删除地址 */
            delete_withdraw_address: function (id) {
                /* 改变颜色 */
                $("#delete").css("color", "#f00b90f");
                /* 添加一个询问弹窗 */
                var content = "你确定要删除这个地址吗？";
                var style = 'border:none; background-color:#fff; color:#333; border-radius:0px'
                layer.open({
                    content: content,
                    btn: ['确定', '取消'],
                    style: style,
                    yes: (index) => {
                        $.post("/btc/delete_withdraw_address", {
                            coin_type: this.coin_type,
                            id: id,
                        }, (res) => {
                            // console.log(res);
                            // alert('删除地址成功！');
                            this.get_withdraw_address(this.coin_type);
                            layer.close(index);
                        });
                    }
                });
            },

            /* 点击的时候把地址添加到地址栏上 */
            push_address: function (index) { 
                // console.log(id);
                // console.log(this.withdraw_address[id].address);
                for(i = 0; i< this.withdraw_address.length; i++){
                    if(i === index){
                        this.inputAddress = this.withdraw_address[i].address;
                    }
                }
            },

            /* 点击提现按钮 */
            withdraw_order: function () {
                $.post("/btc/withdraw_order", {
                    address: this.inputAddress,
                    coin_type: this.coin_type,
                    amount: this.inputNum
                }, (res) => {
                    //成功提示
                    layer.open({
                        content: '提现申请成功！',
                        style: 'color: #333',
                        btn: "确定"
                    }, (res) => {
                        location.reload();
                    });
                });
            },

            /* 跳转到历史记录页面 */
            to_history: function (coin_type) { 
                window.location.href = './history?coin_type=' + coin_type;
            }


        },
		computed: {
			actBtn: function () {
				if(this.inputNum!=''&& this.inputAddress!=''){
					return true;
				}else{
					return false;
				}
            },
            /* 手续费 */
            fee : {
                get: function () {
                    return this.wid_detail.total_fee = (this.inputNum) > 0 ? (this.inputNum * this.wid_detail.fee + this.wid_detail.fee_min) : 0;
                }
            },
            /* 实际到账 */
            got : {
                get: function () {
                    return (this.inputNum) > this.wid_detail.fee_min ? (this.inputNum - this.fee ) : 0;
                }
            }
		}
    });
    vue.init();
}
function vue2_init(coin_type) {
	vue2=new Vue({
		el: "#formContent",
		data: {
			assetsTyple:'',
			markName:'',
			mTxt:'',
			mAddress:'',
            mATxt:'',
            coin_type:coin_type,
		},
		methods:{
			check_mark:function(e){
				var m_val=e.target.value;
				if(m_val==''||m_val==null){
					this.mTxt='该字段不能为空';
				}else{
					this.mTxt='';
				}
			},
			check_mAddress:function(e){
				var m_val=e.target.value;
				if(m_val==''||m_val==null){
					this.mATxt='该字段不能为空';
				}else{
					this.mATxt='';
                }
                
			},
			set_address(){
                this.add_withdraw_address();
				vue.$data.inputAddress=this.mAddress;
				layer.closeAll();
				this.$destroy();
            },
            
            /* 新增地址 */
            add_withdraw_address: function (){
                // console.log("xxx" + this.coin_type)
                $.post("/btc/add_withdraw_address", {
                    coin_type: this.coin_type,
                    address: this.mAddress,
                    detail: this.markName,
                }, (res) => {
                    console.log(res);
                    //成功提示
                    layer.open({
                        content: '新增地址成功',
                        style: 'color: #333',
                        btn: "确定"
                    }, (res) => {
                        location.reload();
                    });
                    
                });
   
            },

		},
		computed: {
			addBtn: function () {
				if(this.markName!=''&& this.mAddress!=''){
					return true;
				}else{
					return false;
				}
			}
		}
	})
}
var CoinList=[
    {"Coin_name":"ADA","L_name":"Cardano"},
    {"Coin_name":"ADA","L_name":"Cardano"},
    {"Coin_name":"ADA","L_name":"Cardano"},
    {"Coin_name":"ADA","L_name":"Cardano"},
    {"Coin_name":"ADA","L_name":"Cardano"},
	{"Coin_name":"ADA","L_name":"Cardano"},
    {"Coin_name":"ADA","L_name":"Cardano"},
    {"Coin_name":"ADA","L_name":"Cardano"},
    {"Coin_name":"ADA","L_name":"Cardano"},
    {"Coin_name":"ADA","L_name":"Cardano"},
	{"Coin_name":"ADA","L_name":"Cardano"},
    {"Coin_name":"ADA","L_name":"Cardano"},
    {"Coin_name":"ADA","L_name":"Cardano"},
    {"Coin_name":"ADA","L_name":"Cardano"},
    {"Coin_name":"ADA","L_name":"Cardano"},
	{"Coin_name":"ADA","L_name":"Cardano"},
    {"Coin_name":"ADA","L_name":"Cardano"},
    {"Coin_name":"ADA","L_name":"Cardano"},
    {"Coin_name":"ADA","L_name":"Cardano"},
    {"Coin_name":"ADA","L_name":"Cardano"},
    ];
function vue3_init() {
	vue3=new Vue({
        el: "#dataList",
        delimiters: ['{!', '!}'],
		data: {
            c_data:CoinList,
            actData: {},
		},
		methods:{
            init: function () { 
                this.get_all_coin();
            },
			select_coin:function(coin_type){
				vue.$data.assetsTyple=coin_type;//VUE间传值
				layer.closeAll();
                this.$destroy();
                window.location.href = "./withdrawal?coin_type=" + coin_type;
            },
            /* 根据类型获取图标和名称 */
            get_all_coin: function() {
                /* 发送请求，获取所有数据 */
                $.post("/btc/get_balances", {}, data => {
                //   console.log(data);d bs 
                  var show_data = [];
                  for (i = 0; i < data.length; i++) {
                    var item = data[i];
                    // console.log(item)
                    var info = {};
                    /* 图标类型 */
                    var coin_info = get_coin_info(item.coin_type);
                    // console.log(coin_info)
                    /* 图标样式 */
                    info.img = get_coin_icon_url(item.coin_type);
                    // console.log("["+info.img+"]")
                    info.coin_type = item.coin_type;
                    info.amount = item.amount;
                    info.name_upper = coin_info.name_upper;
                    info.full_name = coin_info.cname;
                    info.is_hidden = false;
                    show_data.push(info);
                  }
                  this.actData = show_data;
                });
            },
		},
    })
    vue3.init();
}		
$(function () {
    vue_init();
})